﻿using System;
using System.Linq.Expressions;

namespace ClusterGenerator.Utils
{
    public class ExpressionHelper
    {
        public Func<float,float> CreateExpression()
        {
            ParameterExpression value = Expression.Parameter(typeof(float),"value");

            ParameterExpression result = Expression.Parameter(typeof(float),"result");

            var exp = Expression.Lambda<Func<float, float>>(
                Expression.Block(
                    new [] {result},
                    Expression.Assign( result,
                        Expression.Divide(Expression.Constant(1f), 
                            Expression.Add(Expression.Constant(1f), 
                                Expression.Convert(
                                    Expression.Call(
                                        null,
                                        typeof(Math).GetMethod("Exp"),
                                        Expression.Convert(value,typeof(double))
                                    ),typeof(float))
                            )
                        )
                    )
                ), value
            );

            
            return exp.Compile();
        }
    }
}
